package com.lagou.rpc.consumer.client;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component
public class RpcClientManger {
    private List<RpcClient> rpcClients = new ArrayList<>();

    private Iterator<RpcClient> iterator;

    public RpcClientManger(){
        //2.创建RpcClient对象
        RpcClient rpcClient = new RpcClient("127.0.0.1", 8899);
        RpcClient rpcClient1 = new RpcClient("127.0.0.1", 8898);
        rpcClients.add(rpcClient);
        rpcClients.add(rpcClient1);
        iterator = rpcClients.iterator();
    }

    public RpcClient getRpcClient(){
        if(iterator.hasNext()){
            return iterator.next();
        }else {
            iterator = rpcClients.iterator();
            return getRpcClient();
        }
    }

    public void close(){
        for (RpcClient rpcClient : rpcClients) {
            rpcClient.close();
        }
    }
}
